package com.cxyd.service;

import com.cxyd.common.utils.ApiResult;
import com.cxyd.modules.api.dto.RcsResultHK;
import com.cxyd.modules.job.task.agvTask.HKTask;
import com.cxyd.modules.job.task.agvTask.YFTask;
import com.cxyd.modules.job.task.wmsTask.PostWmsTask;
import com.cxyd.modules.wcs.dto.*;
import com.cxyd.modules.wcs.entity.RouteCtuEntity;
import com.cxyd.modules.wcs.service.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ReceiveTaskTestService {
    @Autowired
    private ReceiveTaskService receiveTaskService;
    @Resource
    private PostWmsService postWmsService;
    @Autowired
    private AgvCallbackHkService callbackHkService;
    @Autowired
    private PostWmsTask postWmsTask;
    @Resource
    private TrkService trkService;
    @Resource
    private PlcItemService itemService;
    @Resource
    private RouteCtuService ctuService;
    @Resource
    private UrlMappingService urlService;
    @Resource
    private YFTask yfTask;
    @Resource
    private HKTask hkTask;
    @Resource
    private StnMappingService stnMappingService;

    @Test
    public void analyzeTest() {
        ReceiveTaskDTO dto = new ReceiveTaskDTO();
        dto.setRequestPk("ae02989db5d749818ebb6eb849657299");
        dto.setContNo("B00603");
        dto.setContType("2");
        dto.setFrmPos("900003FJ501013");
        dto.setToPos("200003D7507011");
        dto.setTrkType("IN");
        dto.setReqTime("2024-10-04 16:44:00");
        receiveTaskService.analyzeHandler(dto);
    }

    @Test
    public void callbackHandle() {
        AgvCallbackHkDTO dto = new AgvCallbackHkDTO();
        dto.setReqCode("1541954B96B1112");
        dto.setTaskCode("90b4569c94be452c95dc7464a714592b");
        dto.setReqTime("2024-09-03 10:08:06");
        dto.setCurrentPositionCode("CV01");
        dto.setMapCode("F01");
        dto.setMethod("start");
        dto.setRobotCode("6001");

        RcsResultHK result = callbackHkService.callbackHandle(dto);
        System.out.println("result:" + result);
    }

    @Test
    public void scanAgvTrkTest() {
//        analyzeTask.scanReceiveTask();
    }

    @Test
    public void updateTrkByContNoTest() {
        trkService.updateTrkByContNo("60003");
    }

    @Test
    public void postWmsHandleTest() {
        PostWmsDTO dto = new PostWmsDTO();
        dto.setContNo("600001");
        dto.setRequestPk("c4390882cafd4c49a3d46f2754d1d725");
        dto.setPostSt(0);
        dto.setNoticeType("2");
        dto.setCurPos("HW-01K010201");
        dto.setCode("0");
        dto.setResult("success");

        ApiResult response = new ApiResult().success("success");
        postWmsService.postWmsHandle(dto, response);
    }

    @Test
    public void signalJudgeTest() {
        boolean flag = itemService.signalJudge("60001", "GET", "900001FJ501013");
    }

    @Test
    public void getTaskTypByConditionTest() {
        RouteCtuEntity in = ctuService.getTaskTypByCondition("IN", "900003FJ501013", "200005D7507012");
        System.out.println("result" + in);
    }

    @Test
    public void getUrlByNo() {
//        hkTask.scanTask();
//        yfTask.postTask();
        postWmsTask.scanPost();
//        final PostWmsDTO record = postWmsService.getPostWms();
//        ApiResult respond = new ApiResult().success("success");
//        postWmsService.postWmsHandle(record, respond);
    }

    @Test
    public void trkOperate() {
//        Map<String, Object> params = new HashMap<>();
//        params.put("trkId", "14");
//        params.put("operateType", "DONE");
//        ProcResult procResult = trkService.trkHandler(params);
//        System.out.println(procResult.getMsg());
        stnMappingService.generateStnMapping(22,28,16);
    }

    public static void main(String[] args) {
        List<String> positions = generateStoragePositions(2, 3, 5);
        System.out.println(positions);
    }

    public static List<String> generateStoragePositions(int rows, int cols, int layers) {
        List<String> positions = new ArrayList<>();

        for (int r = 1; r <= rows; r++) {
            for (int c = 1; c <= cols; c++) {
                for (int l = 1; l <= layers; l++) {
                    String position = String.format("HW-01K%02d%02d%02d", r, c, l);
                    positions.add(position);
                }
            }
        }

        return positions;
    }
}
